#!/usr/bin/env bash
#############
tmoe_documents_menu() {
    RETURN_TO_WHERE='tmoe_documents_menu'
    RETURN_TO_MENU='tmoe_documents_menu'
    DEPENDENCY_01=""
    DEPENDENCY_02=""
    TMOE_APP=$(
        whiptail --title "documents" --menu \
            "Which software do you want to install?" 0 50 0 \
            "1" "LibreOffice(开源、自由的办公文档软件)" \
            "2" "WPS office(打造新一代办公环境)" \
            "3" "Chinese manual(中文手册)" \
            "4" "永中Office(x64,秉承集成创新理念)" \
            "5" "Free Office(x86,x64,全面支持微软Office文件)" \
            "6" "Meld(可视化的文本差异比较工具)" \
            "7" "Kdiff3(KDE下的文件差异对比工具)" \
            "0" "🌚 Return to previous menu 返回上级菜单" \
            3>&1 1>&2 2>&3
    )
    ##########################
    case "${TMOE_APP}" in
    0 | "") software_center ;;
    1) install_libre_office ;;
    2) install_wps_office ;;
    3) install_chinese_manpages ;;
    4) yozo_office_env ;;
    5) free_office_env ;;
    6) install_meld ;;
    7) install_kdiff3 ;;
    esac
    ##########################
    press_enter_to_return
    tmoe_documents_menu
}
#############
install_meld() {
    DEPENDENCY_02="meld"
    beta_features_quick_install
}
############
install_kdiff3() {
    DEPENDENCY_02="kdiff3"
    beta_features_quick_install
}
#################
free_office_env() {
    DEPENDENCY_01="freeoffice"
    GREP_NAME="${DEPENDENCY_01}"
    OFFICIAL_URL='https://www.freeoffice.com/download/applications'
    tmoe_app_menu_01
}
############
install_free_office() {
    REPO_URL="${OFFICIAL_URL}"
    THE_LATEST_DEB_URL=$(curl -L ${REPO_URL} | grep 'amd64.deb' | sed 's@ @\n@g' | grep deb | cut -d '"' -f 2 | cut -d '=' -f 2 | head -n 1)
    case ${ARCH_TYPE} in
    amd64) ;;
    i386) THE_LATEST_DEB_URL=$(printf '%s\n' "${THE_LATEST_DEB_URL}" | sed "s@amd64.deb@${ARCH_TYPE}.deb@") ;;
    esac
    #https://www.softmaker.net/down/softmaker-freeoffice-2018_976-01_amd64.deb
    #https://www.softmaker.net/down/softmaker-freeoffice-2018-976.x86_64.rpm
    case ${LINUX_DISTRO} in
    debian | arch) ;;
    redhat)
        case ${ARCH_TYPE} in
        amd64)
            THE_LATEST_DEB_URL="$(curl -L ${REPO_URL} | grep rpm | sed 's@ @\n@g' | grep 'rpm' | grep 'x86_64' | cut -d '"' -f 2 | cut -d '=' -f 2 | head -n 1)"
            #THE_LATEST_DEB_URL=$(printf '%s\n' "${THE_LATEST_DEB_URL}" | sed "s@-01_amd64.deb@.x86_64.rpm@" | sed "s@_amd64.deb@.x86_64.rpm@")
            ;;
        i386)
            THE_LATEST_DEB_URL=$(curl -L ${REPO_URL} | grep 'rpm' | sed 's@ @\n@g' | grep rpm | grep i386 | cut -d '"' -f 2 | cut -d '=' -f 2 | head -n 1)
            #THE_LATEST_DEB_URL=$(printf '%s\n' "${THE_LATEST_DEB_URL}" | sed "s@-01_amd64.deb@.i386.rpm@" | sed "s@_amd64.deb@.i386.rpm@")
            ;;
        esac
        ;;
    esac
    THE_LATEST_DEB_FILE=$(printf '%s\n' "${THE_LATEST_DEB_URL}" | awk -F '/' '{print $NF}')
    THE_LATEST_DEB_VERSION=$(printf '%s\n' "${THE_LATEST_DEB_FILE}" | sed 's@.deb@@' | sed 's@softmaker-freeoffice-@@' | sed "s@${GREP_NAME}_@@")
    check_deb_version
    case ${ARCH_TYPE} in
    amd64) ;;
    i386) ;;
    *) arch_does_not_support ;;
    esac
    download_and_install_deb
}
###################
yozo_office_env() {
    DEPENDENCY_01="yozo-office"
    case ${LINUX_DISTRO} in
    arch) DEPENDENCY_02="yozo-office-fonts" ;;
    esac
    GREP_NAME="${DEPENDENCY_01}"
    OFFICIAL_URL='https://www.yozosoft.com/product-officelinux.html'
    tmoe_app_menu_01
}
############
install_yozo_office() {
    #REPO_URL='http://www.yozosoft.com/product-officelinux.html'
    #https证书错误，因此不加s
    #THE_LATEST_DEB_FILE=$(curl -L ${REPO_URL} | grep 'amd64.deb' | grep '=' | cut -d '=' -f 2 | cut -d '"' -f 2 | awk -F '/' '{print $NF}')
    #THE_LATEST_DEB_URL="https://dl.yozosoft.com/portal-download/fileManager/PRODUCT/${THE_LATEST_DEB_FILE}"
    #arm64) THE_LATEST_DEB_URL=$(printf '%s\n' "${THE_LATEST_DEB_URL}" | sed "s@amd64.deb@arm64.deb@") ;;
    #https://dl.yozosoft.com/portal-download/fileManager/PRODUCT/yozo-office_8.0.1331.101ZH.S1_amd64.deb
    #https://dl.yozosoft.com/portal-download/fileManager/PRODUCT/yozo-office-8.0.1331.101ZH.S1-1.x86_64.rpm
    REPO_URL='https://aur.tuna.tsinghua.edu.cn/packages/yozo-office/'
    THE_LATEST_DEB_URL=$(curl -L ${REPO_URL} | grep deb | cut -d '=' -f 2 | cut -d '"' -f 2 | head -n 1)
    case ${LINUX_DISTRO} in
    debian) ;;
    redhat) THE_LATEST_DEB_URL=$(printf '%s\n' "${THE_LATEST_DEB_URL}" | sed "s@${GREP_NAME}_@${GREP_NAME}-@" | sed "s@_amd64.deb@-1.x86_64.rpm@") ;;
    esac
    THE_LATEST_DEB_FILE=$(printf '%s\n' "${THE_LATEST_DEB_URL}" | awk -F '/' '{print $NF}')
    THE_LATEST_DEB_VERSION=$(printf '%s\n' "${THE_LATEST_DEB_FILE}" | sed 's@.deb@@' | sed "s@${GREP_NAME}-@@" | sed "s@${GREP_NAME}_@@")
    ICON_FILE='/usr/share/icons/hicolor/48x48/apps/yozoicon.png'
    if [ -e "${ICON_FILE}" ]; then
        catimg "${ICON_FILE}" 2>/dev/null
    fi
    check_deb_version
    case ${ARCH_TYPE} in
    amd64) ;;
    *) arch_does_not_support ;;
    esac
    #printf "%s\n" "最新版链接为${BLUE}${THE_LATEST_DEB_URL}${RESET}"
    download_and_install_deb
}
###################
make_font_cache() {
    for i in mkfontscale mkfontdir fc-cache; do
        [[ ! $(command -v ${i}) ]] || ${i}
    done
}
###############
install_wps_office() {
    random_neko
    DEPENDENCY_01="wps-office"
    DEPENDENCY_02=""
    printf "%s\n" "正在检测版本更新..."
    printf "%s\n" "若安装失败，则请前往官网手动下载安装。"
    printf "%s\n" "url: ${YELLOW}https://linux.wps.cn${RESET}"
    case ${LINUX_DISTRO} in
    redhat)
        THE_LATEST_DEB_URL=$(curl -L https://linux.wps.cn/ | grep '\.rpm' | grep -i "$(uname -m)" | head -n 1 | cut -d '=' -f 2 | cut -d '"' -f 2)
        ;;
    *)
        THE_LATEST_DEB_URL=$(curl -L https://linux.wps.cn/ | grep '\.deb' | grep -i "${ARCH_TYPE}" | head -n 1 | cut -d '=' -f 2 | cut -d '"' -f 2)
        ;;
    esac
    THE_LATEST_DEB_VERSION=$(printf '%s\n' "${THE_LATEST_DEB_URL}" | awk -F '/' '{print $NF}' | sed 's@.deb@@')
    TMOE_TIPS_01="检测到最新版本为${THE_LATEST_DEB_VERSION}"
    lolcat_tmoe_tips_01
    printf "%s\n" "最新版链接为${BLUE}${THE_LATEST_DEB_URL}${RESET}"
    if [ ! -e "${APPS_LNK_DIR}/wps-office-wps.desktop" ]; then
        #press_enter_to_reinstall
        printf "%s\n" "未检测到本地版本，您可能尚未安装WPS客户端。"
    elif [ -e "${TMOE_LINUX_DIR}/${DEPENDENCY_01}-version" ]; then
        printf "%s\n" "本地版本可能为${YELLOW}$(head -n 1 ${TMOE_LINUX_DIR}/${DEPENDENCY_01}-version)${RESET}"
        printf "%s\n" "如需${RED}卸载${RESET}，请手动输${BLUE} ${TMOE_REMOVAL_COMMAND} ${DEPENDENCY_01} ${DEPENDENCY_02} ${RESET}"
    else
        printf "%s\n" "未检测到本地版本，您可能不是通过tmoe-linux tool安装的。"
    fi
    do_you_want_to_continue
    cd /tmp
    if [ ! -e "/usr/share/fonts/truetype/wps-fonts/wingding.ttf" ]; then
        aria2c --console-log-level=warn --no-conf --allow-overwrite=true -s 5 -x 5 -k 1M -d /tmp -o ttf-wps-fonts.tar.xz https://gitee.com/ak2/ttf-wps-fonts/raw/master/ttf-wps-fonts.tar.xz
        tar -Jxvf /tmp/ttf-wps-fonts.tar.xz -C /
        rm -fv /tmp/ttf-wps-fonts.tar.xz
        cd /usr/share/fonts/truetype/wps-fonts
        make_font_cache
        cd /tmp
    fi

    if [ ! -e "/usr/share/fonts/truetype/msttcorefonts/Impact.ttf" ]; then
        git clone --depth=1 https://gitee.com/ak2/msttcorefonts.git /tmp/.MS_FONTS_TEMP_FOLDER
        cd /tmp/.MS_FONTS_TEMP_FOLDER
        tar -JPxvf msttcorefonts.tar.xz
        cd /usr/share/fonts/truetype/msttcorefonts
        make_font_cache
        cd /tmp
        rm -rvf .MS_FONTS_TEMP_FOLDER 2>/dev/null
    fi

    for i in /tmp ${HOME}/sd/Download; do
        [[ ! -d ${i} ]] || DEB_DIR=${i}
    done
    cd ${DEB_DIR}
    case "${LINUX_DISTRO}" in
    "debian")
        dpkg --configure -a
        if [ ! -e "/usr/share/doc/ttf-mscorefonts-installer/copyright" ]; then
            printf "%s\n" "apt install -y ttf-mscorefonts-installer"
            apt install -y ttf-mscorefonts-installer
        fi
        aria2c --console-log-level=warn --no-conf --allow-overwrite=true -s 5 -x 5 -k 1M -o WPSoffice.deb "${THE_LATEST_DEB_URL}"
        apt-cache show ./WPSoffice.deb
        apt install -y ./WPSoffice.deb || dpkg -i ./WPSoffice.deb
        printf "%s\n" "You can type ${RED}rm -fv ${BLUE}${DEB_DIR}/WPSoffice.deb${RESET} to delete the deb file."
        ;;
    "arch")
        DEPENDENCY_01="wps-office-cn"
        beta_features_quick_install
        ;;
    "redhat")
        aria2c --console-log-level=warn --no-conf --allow-overwrite=true -s 5 -x 5 -k 1M -o WPSoffice.rpm "${THE_LATEST_DEB_URL}"
        yum install -y ./WPSoffice.rpm
        printf "%s\n" "You can type ${RED}rm -fv ${BLUE}${DEB_DIR}/WPSoffice.rpm${RESET} to delete the rpm file."
        ;;
    esac
    printf "%s\n" "${THE_LATEST_DEB_VERSION}" >${TMOE_LINUX_DIR}/${DEPENDENCY_01}-version
    #rm -fv ./WPSoffice.deb ./WPSoffice.rpm 2>/dev/null
    beta_features_install_completed
}
###################
install_libre_office() {
    #ps -e >/dev/null || printf "%s\n" "/proc分区未挂载，请勿安装libreoffice,赋予proot容器真实root权限可解决相关问题，但强烈不推荐！"
    case ${TMOE_PROOT} in
    no)
        printf "%s\n" "${RED}WARNING！${RESET}检测到您无权读取${GREEN}/proc${RESET}的某些数据！"
        printf "%s\n" "本工具将为此软件自动打补丁以解决无法运行的问题，但无法保证补丁有效。"
        ;;
    esac
    #RETURN_TO_WHERE='software_center'
    #do_you_want_to_continue
    case "${LINUX_DISTRO}" in
    "debian") DEPENDENCY_01='--no-install-recommends libreoffice' ;;
    *) DEPENDENCY_01="libreoffice" ;;
    esac
    DEPENDENCY_02="libreoffice-l10n-zh-cn libreoffice-gtk3"

    beta_features_quick_install
    case "${TMOE_PROOT}" in
    no)
        patch_libreoffice
        printf "%s\n" "打补丁完成"
        ;;
    esac
}
###################
patch_libreoffice() {
    mkdir -pv /prod/version
    cd /usr/lib/libreoffice/program
    rm -f oosplash
    curl -Lo 'oosplash' https://gitee.com/mo2/patch/raw/libreoffice/oosplash
    chmod +x oosplash
}
##################
check_libreoffice_patch() {
    if [ $(command -v libreoffice) ]; then
        patch_libreoffice
    fi
}
############
tmoe_documents_menu
